const { defineConfig } = require('@vue/cli-service')
const path = require('path')

module.exports = defineConfig({
  publicPath: './', // 使用相对路径，解决file协议访问问题
  outputDir: 'dist',
  assetsDir: 'assets',
  configureWebpack: {
    resolve: {
      alias: {
        '@': path.resolve(__dirname, 'src'),
        'vditor': path.resolve(__dirname, 'public/packages/vditor')
      },
      modules: [
        path.resolve(__dirname, 'public/packages'),
        'node_modules'
      ]
    },
    optimization: {
      splitChunks: {
        cacheGroups: {
          vendor: {
            test: /[\\]node_modules[\\]/,
            name: 'vendors',
            chunks: 'all'
          },
          'element-plus': {
            test: /[\\]node_modules[\\]element-plus/,
            name: 'element-plus',
            chunks: 'all'
          },
          'vditor': {
            test: /[\\]node_modules[\\]vditor/,
            name: 'vditor',
            chunks: 'all'
          },
          'screenshot': {
            test: /[\\]node_modules[\\]modern-screenshot/,
            name: 'screenshot',
            chunks: 'all'
          }
        }
      },
      minimize: false // 禁用代码压缩，不使用TerserPlugin
    }
  },
  chainWebpack: config => {
    // 处理静态资源
    config.plugin('copy')
      .tap(args => {
        // 确保复制public/packages目录到dist
        if (args[0].patterns) {
          args[0].patterns.push({
            from: path.resolve(__dirname, 'public/packages'),
            to: path.resolve(__dirname, 'dist/packages'),
            noErrorOnMissing: true
          })
          // 复制主题文件到dist
          args[0].patterns.push({
            from: path.resolve(__dirname, 'public/card-themes'),
            to: path.resolve(__dirname, 'dist/card-themes')
          })
        }
        return args
      })
  }
})